/**
 * @version: 	0.1
 * @author: 	Jasper Jongepier <twicejr@gmail.com>
 * @copyright: 	Jasper Jongepier <twicejr@gmail.com>
 * @slogan: 	SELMe. Selectboxes with onchange url-relocation, without compromising semantics and usability (on older browsers/noscript)
 * @description:SELMe is meant to make a selectbox trigger an action, without compromising older browser without javascript.
 *				Those older browsers without script, get served a regular unordered list.
 *
 * @usage:

	<ul class="SELMe">
		<li><a href="/option-one">Option one</a></li>
		<li class="selected"><a href="/option-two"><span class="this is whatever element strong span i u">Option two is selected</span></a></li>
	</ul>
	
	<script>
		$(".SELMe").SELMe();
	</script>

 **/

(function($)
{
	var opts = {};

	$.fn.SELMe = function(options) 
	{
		settings = $.extend( {}, $.fn.SELMe.defaults, options);
		return this.each(function() 
		{
			var dom = $(this);
			if (dom.data('SELMe'))
				return; // already converted
			dom.data('SELMe', true);
			new SELMe_(settings, dom).initialize();
			$('.SELMe_').change(function(){window.location.replace($(this).attr('value'));});
		});
	};

	$.fn.SELMe.defaults = {};

	function SELMe_(settings, dom) 
	{
		this.settings	= settings;
		this.dom 		= dom;
		this.init		= false;
	};
	
	$.extend(SELMe_.prototype, 
	{	
		initialize: function() 
		{
			this.makeEditor();
		},
		makeEditor: function() 
		{
			if(!this.init)
			{
				select_list = '<select class="SELMe_">';
				$.each(this.dom.children(),function(i, child)
				{
					$(child).hasClass('selected') ? selected = 'selected=selected' : selected = '';
					href	= $(child).find('a').attr('href');
					title	= $(child).find('a').html();
					select_list += '<option value="'+href+'"'+selected+'>' + title + '</option>';
				});
				select_list += '</select>';
				this.dom.parent().html(select_list);
				this.init = true;
			}
		}
	});
	
})(jQuery);